#include "CollisionObject.h"

CollisionObject::CollisionObject()
{
	width = 0;
	height = 0;
}

CollisionObject::CollisionObject(int w, int h)
{
	width = w;
	height = h;
}

CollisionObject::~CollisionObject(void)
{
}

int CollisionObject::getWidth(){ return width; }
int CollisionObject::getHeight(){ return height; }

Vector2d CollisionObject::collide(float xCalled, float yCalled, CollisionObject *o, float xOPos, float yOPos)
{
	return rectsOverlap(xCalled, yCalled, width, height, xOPos, yOPos, o->width, o->height);
}


Vector2d rectsOverlap(float x1, float y1, int w1, int h1, float x2, float y2, int w2, int h2)
{
	x1-= w1/2;
	y1-= h1/2;
	x2-= w2/2;
	y2-= h2/2;
	if(!(x1+w1 > x2 && x1 < x2+w2 && y1+h1 > y2 && y1 < y2+h2))
		return Vector2d();

	float xDepth = (x1 + w1/2) - (x2 + w2/2);
	float yDepth = (y1 + h1/2) - (y2 + h2/2);

	float xReturn = w1/2 + w2/2 - xDepth;
	float yReturn = h1/2 + h2/2 - yDepth;

	if(xDepth < 0) xReturn = - (w1/2 + w2/2 + xDepth);
	if(yDepth < 0) yReturn = - (h1/2 + h2/2 + yDepth);

	return Vector2d(xReturn, yReturn);
}




/*
Function MegaRectsOverlap:vector(x0:Int, y0:Int, w0:Int, h0:Int, x2:Int, y2:Int, w2:Int, h2:Int)
    	If x0 > (x2 + w2) Or (x0 + w0) < x2 Then Return Null
    	If y0 > (y2 + h2) Or (y0 + h0) < y2 Then Return Null
	Local x:Int, y:Int
	Local v:vector
	Local xPenetrationDepth:Float = ((x0+w0/2)-(x2+w2/2))
	Local yPenetrationDepth:Float = ((y0+h0/2)-(y2+h2/2))
	If xPenetrationDepth < 0 Then x = -((w0/2)+(w2/2))-((x0+w0/2)-(x2+w2/2))
	If xPenetrationDepth > 0 Then x =  ((w0/2)+(w2/2))-((x0+w0/2)-(x2+w2/2))
	If yPenetrationDepth < 0 Then y = -((h0/2)+(h2/2))-((y0+h0/2)-(y2+h2/2))
	If yPenetrationDepth > 0 Then y = ((h0/2)+(h2/2))-((y0+h0/2)-(y2+h2/2))
	v:vector = vector.Create(x,y)
	Return v
End Function
*/

bool rectanglesOverlap(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
{
	return x1+w1 >= x2 && x1 <= x2+w2 && y1+h1 >= y2 && y1 <= y2+h2;
}